#include "MainWindow.h"
#include "ui_MainWindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            s[i][j]=0;
        }
    }
    button = new QPushButton("开始脱发换学分。",this);
    button->setGeometry(60,400,200,50);//从(60,400)做一个200*50的按钮

    qsrand(uint(QTime(0,0,0).secsTo(QTime::currentTime())));

    connect(button,SIGNAL(clicked()),this,SLOT(slotStart()));

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.drawPixmap(rect(),QPixmap("F:\\qt\\2048\\try\\lys.jpg"),QRect());		//传入资源图片路径

    QPainter p(this);
    p.setBrush(Qt::black);
    p.setFont(QFont("楷体",20,700,false));

    QString strscore;
    p.drawText(QPoint(200,90),"分数: "+QString::number(score));


    for (int i=0;i<4;i++) {
        for (int j=0;j<4;j++) {
            p.setPen(Qt::transparent);//?
            if(s[i][j]==0){
                p.setBrush(QBrush(QColor(230,255,255,50)));
                p.drawRect(i*60+40,j*60+120,55,55);
            }

            else if (s[i][j]==2) {
                p.setBrush(QBrush(QColor(204,202,202,150)));
                p.drawRect(i*60+40,j*60+120,55,55);
                p.setPen(Qt::black);
                p.setFont(QFont("楷体",10,700,false));

                p.drawText(QRectF(i*60+40,j*60+120,55,80),"循环",QTextOption(Qt::AlignCenter));
                p.setPen(Qt::black);
                p.setFont(QFont("华文琥珀",7,700,false));
                p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(2),QTextOption(Qt::AlignBottom));
            }

            else if (s[i][j]==4) {
                p.setBrush(QBrush(QColor(195,244,249,190)));
                p.drawRect(i*60+40,j*60+120,55,55);
                p.setPen(Qt::red);
                p.setFont(QFont("楷体",10,700,false));

                p.drawText(QRectF(i*60+40,j*60+120,55,55),"数组",QTextOption(Qt::AlignCenter));
                p.setPen(Qt::black);
                p.setFont(QFont("华文琥珀",7,700,false));
                p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(4),QTextOption(Qt::AlignBottom));
            }


            else if (s[i][j]==8) {
                p.setBrush(QBrush(QColor(164,246,255,190)));
                p.drawRect(i*60+40,j*60+120,55,55);
                p.setPen(Qt::red);
                p.setFont(QFont("楷体",10,700,false));

                p.drawText(QRectF(i*60+40,j*60+120,55,55),"函数",QTextOption(Qt::AlignCenter));
                p.setPen(Qt::black);
                p.setFont(QFont("华文琥珀",7,700,false));
                p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(8),QTextOption(Qt::AlignBottom));
            }

            else if (s[i][j]==16) {
                p.setBrush(QBrush(QColor(132,213,229,190)));
                p.drawRect(i*60+40,j*60+120,55,55);
                p.setPen(Qt::red);
                p.setFont(QFont("楷体",10,700,false));

                p.drawText(QRectF(i*60+40,j*60+120,55,55),"结构",QTextOption(Qt::AlignCenter));
                p.setPen(Qt::black);
                p.setFont(QFont("华文琥珀",7,700,false));
                p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(16),QTextOption(Qt::AlignBottom));
            }

            else if (s[i][j]==32) {
                p.setBrush(QBrush(QColor(91,168,240,170)));
                p.drawRect(i*60+40,j*60+120,55,55);
                p.setPen(QPen(QColor(0,255,38,150)));
                p.setFont(QFont("楷体",10,700,false));

                p.drawText(QRectF(i*60+40,j*60+120,55,55),"指针",QTextOption(Qt::AlignCenter));
                p.setPen(Qt::black);
                p.setFont(QFont("华文琥珀",7,700,false));
                p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(32),QTextOption(Qt::AlignBottom));
            }

            else if (s[i][j]==64) {
                p.setBrush(QBrush(QColor(66,169,230,180)));
                p.drawRect(i*60+40,j*60+120,55,55);
                p.setPen(Qt::red);
                p.setFont(QFont("楷体",10,700,false));

                p.drawText(QRectF(i*60+40,j*60+120,55,55),"类",QTextOption(Qt::AlignCenter));
                p.setPen(Qt::black);
                p.setFont(QFont("华文琥珀",7,700,false));
                p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(64),QTextOption(Qt::AlignBottom));
            }

            else if (s[i][j]==128) {
                p.setBrush(QBrush(QColor(40,104,195,190)));
                p.drawRect(i*60+40,j*60+120,55,55);
                p.setPen(Qt::red);
                p.setFont(QFont("楷体",9,700,false));

                p.drawText(QRectF(i*60+40,j*60+120,55,55),"栈与队列",QTextOption(Qt::AlignCenter));
                p.setPen(Qt::black);
                p.setFont(QFont("华文琥珀",7,700,false));
                p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(128),QTextOption(Qt::AlignBottom));
            }

            else if (s[i][j]==256) {
                p.setBrush(QBrush(QColor(75,104,233,180)));
                p.drawRect(i*60+40,j*60+120,55,55);
                p.setPen(Qt::red);
                p.setFont(QFont("楷体",10,700,false));

                p.drawText(QRectF(i*60+40,j*60+120,55,55),"继承",QTextOption(Qt::AlignCenter));
                p.setPen(Qt::black);
                p.setFont(QFont("华文琥珀",7,700,false));
                p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(256),QTextOption(Qt::AlignBottom));
            }

            else if (s[i][j]==512) {
                p.setBrush(QBrush(QColor(34,78,173,180)));
                p.drawRect(i*60+40,j*60+120,55,55);
                p.setPen(Qt::red);
                p.setFont(QFont("楷体",10,700,false));

                p.drawText(QRectF(i*60+40,j*60+120,55,55),"多态",QTextOption(Qt::AlignCenter));
                p.setPen(Qt::black);
                p.setFont(QFont("华文琥珀",7,700,false));
                p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(512),QTextOption(Qt::AlignBottom));
            }

            else if (s[i][j]==1024) {
                p.setBrush(QBrush(QColor(19,40,173,180)));
                p.drawRect(i*60+40,j*60+120,55,55);
                p.setPen(Qt::red);
                p.setFont(QFont("楷体",10,700,false));

                p.drawText(QRectF(i*60+40,j*60+120,55,55),"QT",QTextOption(Qt::AlignCenter));
                p.setPen(Qt::black);
                p.setFont(QFont("华文琥珀",7,700,false));
                p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(1024),QTextOption(Qt::AlignBottom));
            }

            else if (s[i][j]==2048) {
                p.setBrush(QBrush(QColor(8,137,223,180)));
                p.drawRect(i*60+40,j*60+120,55,55);
                p.setPen(Qt::red);
                p.setFont(QFont("楷体",10,700,false));

                p.drawText(QRectF(i*60+40,j*60+120,55,55),"大作业",QTextOption(Qt::AlignCenter));
                p.setPen(Qt::black);
                p.setFont(QFont("华文琥珀",7,700,false));
                p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(2048),QTextOption(Qt::AlignBottom));
            }

            else{
                p.setBrush(Qt::darkBlue);
                p.drawRect(i*60+40,j*60+120,55,55);
                p.setPen(Qt::black);
                p.setFont(QFont("华文琥珀",10,700,false));
                p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(s[i][j]),QTextOption(Qt::AlignCenter));
            }

        }
    }
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    if(!state){
        return ;
    }


        switch (event->key()) {
        case Qt::Key_W:
            PressUp();        myRand();
            break;

        case Qt::Key_S:
            PressDown();        myRand();
            break;

        case Qt::Key_A:
            PressLeft();        myRand();
            break;

        case Qt::Key_D:
            PressRight();        myRand();
            break;

        case Qt::Key_0:{

            QPixmap pixmap2(":/music/C:/Users/Lenovo/Desktop/新建文件夹/0g.jpg");   //设置画面
              QSplashScreen splash2(pixmap2);
              splash2.show();

              QElapsedTimer t;
              t.start();
              while(t.elapsed()<500)
              {
                  QCoreApplication::processEvents();
              }

             QMessageBox::about(this,"你已被列入暗杀名单","今日留一物，他日好相见，重新输入");


        }
             break;

        case Qt::Key_1:{
            QPixmap pixmap2(":/music/C:/Users/Lenovo/Desktop/新建文件夹/1g.jpg");   //设置画面
              QSplashScreen splash2(pixmap2);
              splash2.show();

              QElapsedTimer t;
              t.start();
              while(t.elapsed()<500)
              {
                  QCoreApplication::processEvents();
              }


                QMessageBox::about(this,"呜呜呜","好无情，换个数字不好吗");
                break;
}
            case Qt::Key_2:
            case Qt::Key_3:
            case Qt::Key_4:
            case Qt::Key_5:
        {
            QPixmap pixmap2(":/music/C:/Users/Lenovo/Desktop/新建文件夹/55.jpg");   //设置画面
                 QSplashScreen splash2(pixmap2);
                 splash2.show();

                 QElapsedTimer t;
                 t.start();
                 while(t.elapsed()<500)
                 {
                     QCoreApplication::processEvents();
                 }
              QMessageBox::about(this,"FBI警告","搞错了，重新输入");
        }
            break;


            case Qt::Key_6:
            case Qt::Key_7:
            case Qt::Key_8:
        {
            QPixmap pixmap2(":/music/C:/Users/Lenovo/Desktop/新建文件夹/88.jpg");   //设置画面
                 QSplashScreen splash2(pixmap2);
                 splash2.show();

                 QElapsedTimer t;
                 t.start();
                 while(t.elapsed()<500)
                 {
                     QCoreApplication::processEvents();
                 }


                QMessageBox::about(this,"发射爱心","再多给一分嘛");
                break;
}

            case Qt::Key_9:
                {

                     QPixmap pixmap2(":/music/C:/Users/Lenovo/Desktop/新建文件夹/999.jpg");   //设置画面
                          QSplashScreen splash2(pixmap2);
                          splash2.show();

                          QElapsedTimer t;
                          t.start();
                          while(t.elapsed()<500)
                          {
                              QCoreApplication::processEvents();
                          }


                QMessageBox::about(this,"感激涕零","祝你c++一百分，好人一生平安你可以继续游戏啦");
                        break;
        }
        case Qt::Key_Z:
            QMessageBox::about(this,"答案之书","愿山野都有雾灯");
                    break;

        case Qt::Key_X:
            QMessageBox::about(this,"答案之书","为什么不呢？");
                    break;
        case Qt::Key_C:
            QMessageBox::about(this,"答案之书","千万不要！！");
                    break;

        case Qt::Key_V:
            QMessageBox::about(this,"答案之书","无远弗届。");
                    break;

        case Qt::Key_B:
            QMessageBox::about(this,"答案之书","你会心想事成");
                    break;

        case Qt::Key_N:
            QMessageBox::about(this,"答案之书","终己不顾，甘之如饴");
                    break;
        case Qt::Key_M:
            QMessageBox::about(this,"答案之书","你要快乐。");
                    break;


        default:
            return;//忽略其他按钮
        }

        update();
}
void MainWindow::slotStart(){
    QMessageBox::about(this," ","游戏过程中你可以尝试在键盘上按下你想给我的分数(0~9）");
    score=0;
    for (int i=0;i<4;i++) {
        for (int j=0;j<4;j++) {
            s[i][j]=0;
        }
    }
    button->setText("重修一年");

    int randi=qrand()%4;
    int randj=qrand()%4;
    s[randi][randj]=2;

    state=true;
    update();
}
void MainWindow::PressUp(){
    //移动
    for (int i=0;i<4;i++) {
        for (int j=1;j<4;j++) {
            if(s[i][j]==0)continue;
            for (int p=0;p<j;p++) {
                //查看前面是否有空格子可移动
                if(s[i][p]==0){
                    s[i][p]=s[i][j];
                    s[i][j]=0;
                    break;
                }
            }
        }
    }
    //相加
    for (int i=0;i<4;i++) {
        for (int j=0;j<3;j++) {
            if(s[i][j]==s[i][j+1]){
                s[i][j]=2*s[i][j];
                s[i][j+1]=0;
                score+=s[i][j];
                for (int p=j+2;p<4;p++) {
                    s[i][p-1]=s[i][p];
                }
            }
        }
    }
}
void MainWindow::PressDown(){
    //移动
    for (int i=0;i<4;i++) {
        for (int j=2;j>=0;j--) {
            if(s[i][j]==0)continue;
            for (int p=3;p>j;p--) {
                //查看前面是否有空格子可移动
                if(s[i][p]==0){
                    s[i][p]=s[i][j];
                    s[i][j]=0;
                    break;
                }
            }
        }
    }
    //相加
    for (int i=0;i<4;i++) {
        for (int j=3;j>0;j--) {
            if(s[i][j]==s[i][j-1]){
                s[i][j]=2*s[i][j];
                s[i][j-1]=0;
                score+=s[i][j];
                for (int p=j-2;p>=0;p--) {
                    s[i][p+1]=s[i][p];
                }
            }
        }
    }
}
void MainWindow::PressLeft(){
    //移动
    for (int j=0;j<4;j++) {
        for (int i=1;i<4;i++) {
            if(s[i][j]==0){
                continue;
            }
            for (int p=0;p<i;p++) {
                //查看前面是否有空格可移入
                if(s[p][j] == 0){
                    s[p][j] = s[i][j];
                    s[i][j] = 0;
                    break;
                }
            }
        }
    }
    //相加
    for (int j=0;j<4;j++) {
        for (int i=0;i<3;i++) {
            if(s[i][j]==s[i+1][j]){
                s[i][j]=s[i][j]*2;
                score+=s[i][j];
                s[i+1][j]=0;
                for(int p=i+2;p<4;p++){
                    s[p-1][j] = s[p][j];
               }
            }
        }
    }
}
void MainWindow::PressRight(){
    //移动
    for (int j=0;j<4;j++) {
        for (int i=2;i>=0;i--) {
            if(s[i][j]==0){
                continue;
            }
            for (int p=3;p>i;p--) {
                //查看前面是否有空格可移入
                if(s[p][j] == 0){
                    s[p][j] = s[i][j];
                    s[i][j] = 0;
                    break;
                }
            }
        }
    }
    //相加
    for (int j=0;j<4;j++) {
        for (int i=3;i>=0;i--) {
            if(s[i][j]==s[i-1][j]){
                s[i][j]=s[i][j]*2;
                s[i-1][j]=0;
                score+=s[i][j];
                for(int p=i-2;p>=0;p--){
                    s[p+1][j] = s[p][j];
                }
            }
        }
    }
}
void MainWindow::myRand(){
    int i=0,j=0;
    //找出格子
    struct Ns n[15];
    int ni=0;

    for (i=0;i<4;i++) {
        for (j=0;j<4;j++) {
            if(s[i][j]==0){
                n[ni].i=i;
                n[ni].j=j;
                ni++;
            }

            else if(s[i][j]==2048){
                ni=2048;
                break;
            }

        }
         if(ni==2048){break;}
    }

    //判断游戏是否结束
    if (ni==0) {
        for (i=0;i<4;i++) {
            for (j=0;j<3;j++) {
                if(s[i][j]==s[i][j+1]){
                    return;
                }
            }
        }
        for (j=0;j<4;j++) {
            for (i=0;i<3;i++) {
                if(s[i][j]==s[i+1][j]){
                    return;
                }
            }
        }

        QMessageBox::about(this,"你挂课啦！！","分数为："+QString::number(score)+"重修吧-3-");
        return;
    }

    if (ni==2048) {

             QPixmap pixmap2(":/music/C:/Users/Lenovo/Desktop/新建文件夹/9.gif");   //设置画面
             QSplashScreen splash2(pixmap2);
             splash2.show();

             QElapsedTimer t;
             t.start();
             while(t.elapsed()<500)
             {
                 QCoreApplication::processEvents();
             }


        QMessageBox::about(this,"成功合成大作业！","恭喜你离满绩又进了一步！游戏分数： "+QString::number(score));
        return;
    }
    int rand=qrand()%ni;
    s[n[rand].i][n[rand].j]=2;

}
